package org.flashcards;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

/**
 *
 * @author siarhei
 */
public class QuizActivity extends Activity {

    private Cursor dictCur;

    private int index = -1;
    private boolean isMarked;

    private boolean isTranslationShown;

    private TextView textView;
    private Button markButton;

    private Uri dictionaryUri;
    
    @Override
    public void onCreate(Bundle icicle) {
        Log.d(getClass().getSimpleName(), "onCreate()");
        super.onCreate(icicle);
        setContentView(R.layout.quiz);

        dictionaryUri = Uri.parse("content://org.dictionary.DictionaryProvider/quiz");
        dictCur = getContentResolver().query(dictionaryUri, null, null, null, null);

        if (!dictCur.moveToFirst()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("No expired dictionary entries.")
                    .setCancelable(false)
                    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            closeActivity();
                        }
                   });
            AlertDialog alert = builder.create();
            alert.show();
            return;
        }

        textView = (TextView)findViewById(R.id.text);

        markButton = (Button)findViewById(R.id.mark_entry);
        markButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                isMarked = !isMarked;
                if (!isMarked) {
                    markButton.setText(R.string.dont_know);
                    textView.setTextColor(Color.WHITE);
                } else {
                    markButton.setText(R.string.know);
                    textView.setTextColor(Color.YELLOW);
                }
            }
        });
        
        showNextItem();
    }

    private void initState() {
        isMarked = false;
        isTranslationShown = true;

        markButton.setText(R.string.dont_know);
        textView.setTextColor(Color.WHITE);
    }

    private void nextItem() {
        ContentValues values = new ContentValues();
        values.put(DictionaryEntry._ID, dictCur.getLong(dictCur.getColumnIndex(DictionaryEntry._ID)));
        if (isMarked) {
            values.put(DictionaryEntry.BATCH, 0);
        } else {
            values.put(DictionaryEntry.BATCH, dictCur.getLong(dictCur.getColumnIndex(DictionaryEntry.BATCH)) + 1);
        }
        getContentResolver().update(dictionaryUri, values, null, null);
        if (dictCur.isLast()) {
            dictCur.close();
            finish();
        } else {
            showNextItem();
        }
    }


    private void closeActivity() {
        dictCur.close();
        finish();
    }

    private void showNextItem() {
        index++;
        showItem();
    }

    private void showItem() {
        dictCur.moveToPosition(index);
        textView.setText(dictCur.getString(dictCur.getColumnIndex(DictionaryEntry.TRANSLATED_TEXT)));
        initState();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.finish:
                dictCur.close();
                finish();
                return true;

            default:
                return false;
        }
    }

    private float x;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            x = event.getX();
            //Log.e(getClass().getSimpleName(), "DOWN: " + event.getX() + ", " + event.getY());
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (Math.abs(x - event.getX()) > 90) {
                nextItem();
            } else {
                isTranslationShown = !isTranslationShown;
                if (isTranslationShown) {
                    textView.setText(dictCur.getString(dictCur.getColumnIndex(DictionaryEntry.TRANSLATED_TEXT)));
                } else {
                    textView.setText(dictCur.getString(dictCur.getColumnIndex(DictionaryEntry.ORIGINAL_TEXT)));
                }
            }
            //Log.e(getClass().getSimpleName(), "UP: " + event.getX() + ", " + event.getY());
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            //Log.e(getClass().getSimpleName(), "MOVE: " + event.getX() + ", " + event.getY());
        }
        return true;
    }

    
}
